package com.zboot.zbootadmin.project.system.menu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zboot.zbootadmin.common.annotation.Log;
import com.zboot.zbootadmin.common.constant.HttpStatus;
import com.zboot.zbootadmin.common.domain.ResponseData;
import com.zboot.zbootadmin.common.security.LoginUser;
import com.zboot.zbootadmin.common.security.service.TokenService;
import com.zboot.zbootadmin.common.utils.ServletUtils;
import com.zboot.zbootadmin.common.utils.StringUtils;
import com.zboot.zbootadmin.project.system.menu.entity.SysMenu;
import com.zboot.zbootadmin.project.system.menu.entity.SysMenuDto;
import com.zboot.zbootadmin.project.system.menu.entity.Tree;
import com.zboot.zbootadmin.project.system.menu.entity.vo.MenuVo;
import com.zboot.zbootadmin.project.system.menu.entity.vo.RouterVo;
import com.zboot.zbootadmin.project.system.menu.service.SysMenuService;
import com.zboot.zbootadmin.project.system.user.domain.SysUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author zyt
 * @since 2020-07-18
 */
@RestController
@RequestMapping("/sysMenu")
public class SysMenuController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysMenuService sysMenuService;

    /**
     *
     * @Description: 构建动态菜单
     *
     * @author: zyt
     * @date: 22:16 2020/9/7
     * @param: []
     * @return: com.zboot.zbootadmin.common.domain.ResponseData
     *
     */
    @GetMapping("/getMenu")
    public ResponseData getMenu() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        if (user.getIsAdmin()==0) {
            List<SysMenu> sysMenuList = sysMenuService.selectMenuList();
            List<SysMenu> sysMenus = sysMenuService.getMenuList(0L,sysMenuList);

            List<RouterVo> routerVos = sysMenuService.buildMenu(sysMenus);
            return new ResponseData(HttpStatus.SUCCESS,routerVos);
        }else{
            List<SysMenu> sysMenuList = sysMenuService.selectMenuListByUserId(user.getUserId().toString());
            List<SysMenu> sysMenus = sysMenuService.getMenuList(0L,sysMenuList);
            List<RouterVo> routerVos = sysMenuService.buildMenu(sysMenus);
            return new ResponseData(HttpStatus.SUCCESS,routerVos);
        }

    }

    /**
     *
     * @Description: 根据用户获取菜单
     *
     * @author: zyt
     * @date: 22:18 2020/9/7
     * @param: []
     * @return: com.zboot.zbootadmin.common.domain.ResponseData
     *
     */
    @GetMapping("/showMenuListByUserId")
    public ResponseData showMenuListByUserId() {
        // 用户信息
        SysUser user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        List<SysMenu> menus;
        if(user.getIsAdmin()==0) {
             menus = sysMenuService.selectMenuList();
        }else{
             menus = sysMenuService.selectMenuListByUserId(user.getUserId().toString());
        }
        List<SysMenu> sysMenus = sysMenuService.getMenuList(0L,menus);
        List<MenuVo> menuVoList = sysMenuService.showMenuList(sysMenus);
        return new ResponseData(HttpStatus.SUCCESS,menuVoList);

    }

    /**
     *
     * @Description: 菜单查询
     *
     * @author: zyt
     * @date: 22:20 2020/9/7
     * @param: [menuName]
     * @return: com.zboot.zbootadmin.common.domain.ResponseData
     *
     */
    @Log("查询菜单")
    @PreAuthorize("@zboot.hasPermi('sys:menu:search')")
    @GetMapping("/search/{menuName}")
    public ResponseData search(@PathVariable("menuName") String menuName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("menu_name",menuName);
        List<SysMenu> sysMenus = sysMenuService.list(queryWrapper);
        return new ResponseData(HttpStatus.SUCCESS, "返回成功",sysMenuService.buildMenu(sysMenus));
    }

    /**
     *
     * @Description: 前端下拉树选项
     *
     * @author: zyt
     * @date: 22:20 2020/9/7
     * @param: []
     * @return: com.zboot.zbootadmin.common.domain.ResponseData
     *
     */
    @GetMapping("/getTree")
    public ResponseData getTree() {
        List<SysMenu> menus = sysMenuService.selectMenuList();
        List<SysMenu> sysMenus = sysMenuService.getMenuList(0L,menus);
        List<RouterVo> routerVos = sysMenuService.buildMenu(sysMenus);
        List<Tree> treeList = routerVos.stream().map(Tree::new).collect(Collectors.toList());
        return new ResponseData(HttpStatus.SUCCESS, treeList);
    }

    /**
     *
     * @Description: 角色菜单中对话框中的权限菜单
     * 
     * @author: zyt
     * @date: 20:13 2020/11/1 
     * @param: []
     * @return: com.zboot.zbootadmin.common.domain.ResponseData
     *
     */
    @GetMapping("/getTreeMenu")
    public ResponseData getTreeMenu() {
        List<SysMenu> sysMenus = sysMenuService.getMenuList(0L,sysMenuService.selectMenuList());
        List<Tree> treeList = sysMenus.stream().map(Tree::new).collect(Collectors.toList());
        return new ResponseData(HttpStatus.SUCCESS, treeList);
    }

    /**
     *
     * @Description: 新增菜单
     *
     * @author: zyt
     * @date: 22:20 2020/9/7
     * @param: [sysMenu]
     * @return: com.zboot.zbootadmin.common.domain.ResponseData
     *
     */
    @Log("新增菜单")
    @PreAuthorize("@zboot.hasPermi('sys:menu:add')")
    @PostMapping("/addMenu")
    public ResponseData addMenu(@RequestBody SysMenuDto sysMenu) {
        SysMenu menu = new SysMenu();
        BeanUtils.copyProperties(sysMenu,menu);
        sysMenuService.addMenu(menu);
        return new ResponseData(HttpStatus.SUCCESS, "添加成功");

    }

    /**
     *
     * @Description: 根据menuid获取菜单
     *
     * @author: zyt
     * @date: 22:21 2020/9/7
     * @param: [menuId]
     * @return: com.zboot.zbootadmin.common.domain.ResponseData
     *
     */
    @GetMapping("/getMenuByMenuId/{menuId}")
    public ResponseData getMenuByMenuId(@PathVariable("menuId") String menuId) {
        SysMenu sysMenu = sysMenuService.getById(menuId);
        return new ResponseData(HttpStatus.SUCCESS, sysMenu);
    }

    /**
     *
     * @Description: 删除菜单，包括父级菜单
     *
     * @author: zyt
     * @date: 22:21 2020/9/7
     * @param: [sysMenu]
     * @return: com.zboot.zbootadmin.common.domain.ResponseData
     *
     */
    @Log("删除菜单")
    @PreAuthorize("@zboot.hasPermi('sys:menu:delete')")
    @DeleteMapping("/deleteMenuAdmin")
    public ResponseData deleteMenuAdmin(@RequestBody SysMenu sysMenu) {
        List<SysMenu> sysMenus = sysMenuService.queryByParentId(sysMenu,sysMenu.getParentId());
        List<String> ids = new ArrayList<>();
        sysMenus.stream().forEach(menu->ids.add(menu.getMenuId().toString()));
        sysMenuService.removeByIds(ids);
        sysMenuService.removeById(sysMenu.getMenuId());
        return new ResponseData(HttpStatus.SUCCESS, "删除成功");
    }

    /**
     *
     * @Description: 更新菜单
     *
     * @author: zyt
     * @date: 22:21 2020/9/7
     * @param: [sysMenu]
     * @return: com.zboot.zbootadmin.common.domain.ResponseData
     *
     */
    @PutMapping("/updateMenu")
    public ResponseData updateMenu(@RequestBody SysMenu sysMenu) {
        sysMenuService.saveOrUpdate(sysMenu);
        return new ResponseData(HttpStatus.SUCCESS, "修改成功");
    }



}

